package com.greensam.pixelengine.controller;

import com.greensam.pixelengine.annotation.AuthCheck;
import com.greensam.pixelengine.pojo.base.BaseResponse;
import com.greensam.pixelengine.pojo.base.DeleteRequest;
import com.greensam.pixelengine.pojo.base.PaginationResponse;
import com.greensam.pixelengine.exception.BusinessException;
import com.greensam.pixelengine.exception.ResultCode;
import com.greensam.pixelengine.pojo.vo.user.request.UserPageRequest;
import com.greensam.pixelengine.pojo.vo.user.request.UserSaveRequest;
import com.greensam.pixelengine.pojo.vo.user.request.UserLoginRequest;
import com.greensam.pixelengine.pojo.vo.user.request.UserRegisterRequest;
import com.greensam.pixelengine.pojo.emuns.ErrorCodeEnum;
import com.greensam.pixelengine.exception.ThrowUtils;
import com.greensam.pixelengine.facade.UserFacadeService;
import com.greensam.pixelengine.pojo.emuns.RoleEnum;
import com.greensam.pixelengine.pojo.entity.UserPo;
import com.greensam.pixelengine.pojo.vo.user.response.LoginUserVo;
import com.greensam.pixelengine.pojo.vo.user.response.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 用户模块
 *
 * @author Macro_Ray
 * @since 2025/6/13 16:38
 */
@Tag(name = "UserController", description = "用户模块")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserFacadeService userFacadeService;

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest request) {
        ThrowUtils.throwIf(Objects.isNull(request), ErrorCodeEnum.ParamValidError);
        return BaseResponse.success(userFacadeService.userRegister(request));
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public BaseResponse<LoginUserVo> userLogin(@RequestBody UserLoginRequest param, HttpServletRequest request) {
        ThrowUtils.throwIf(Objects.isNull(param), ErrorCodeEnum.ParamValidError);
        return BaseResponse.success(userFacadeService.userLogin(param, request));
    }

    @Operation(summary = "用户登出")
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        return BaseResponse.success(userFacadeService.userLogout(request));
    }

    @Operation(summary = "新增/修改用户")
    @PostMapping("/save")
    public BaseResponse<Long> saveUser(HttpServletRequest httpServletRequest, @RequestBody UserSaveRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        UserPo userPo = userFacadeService.saveUser(request, loginUser.getId());
        if (Objects.isNull(userPo)) {
            return BaseResponse.failed(ResultCode.OPERATION_ERROR);
        }
        return BaseResponse.success(userPo.getId());
    }

    @Operation(summary = "删除用户")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(HttpServletRequest httpServletRequest, @RequestBody @Valid DeleteRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(userFacadeService.deleteUser(request, loginUser.getId()));
    }

    @Operation(summary = "获取当前登录用户")
    @GetMapping("/login/get")
    public BaseResponse<LoginUserVo> getLoginUser(HttpServletRequest request) {
        return BaseResponse.success(userFacadeService.getLoginUserVo(request));
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/get/po")
    @AuthCheck(mustRole = {RoleEnum.ADMIN})
    public BaseResponse<UserPo> getUserById(@RequestParam("id") @Parameter(description = "用户ID") Long id) {
        return BaseResponse.success(userFacadeService.getUserById(id)
                .orElseThrow(() -> new BusinessException(ResultCode.OPERATION_ERROR)));
    }

    @Operation(summary = "获取用户信息（脱敏）")
    @GetMapping("/get")
    public BaseResponse<UserVo> getUserVoById(@RequestParam("id") @Parameter(description = "用户ID") Long id) {
        return BaseResponse.success(userFacadeService.getUserVo(id));
    }

    @Operation(summary = "分页查询用户信息（脱敏）")
    @PostMapping("/page")
    @AuthCheck(mustRole = {RoleEnum.ADMIN})
    public BaseResponse<PaginationResponse<UserVo>> pageUser(@RequestBody UserPageRequest request) {
        return BaseResponse.success(userFacadeService.pageUser(request));
    }
}
